<template>
	<div id = 'department'>
		<h3 style="padding:0 0 15px 15px">{{ isEdit ? "编辑科室" : "新增科室"}}</h3>
		<el-form label-width="100px">
			<el-form-item
						v-for="(field, index) in fields"
						v-if="!field.noSave &&!field.noFormDisplay"
						:label="field.label"
						:prop="field.tableName"
						:key="index"
						:rules="field.rules"
						>
					<wt-model-field-input
							:ref="field.tableName"
							:field="field"
							:form="form"
							:params-data="params_data"
							:class="{'wt-model-inline-field' : field.conditions}"
							v-on:grade = "change"
							></wt-model-field-input>
					<span class="wt-extra" v-if="field.extra">{{field.extra}}</span>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="onSave" :loading="saving" icon="check">保存</el-button>
					<el-button @click="onCancel" icon="close">取消</el-button>
				</el-form-item>
		</el-form>
	</div>
</template>
<script>
	import Vue from 'vue'
	import ElementUI from 'element-ui'
	import request from 'winged/utils/axios'
	import Fields from 'winged/utils/wt-model-field'
	import Permissions from 'winged/utils/wt-permission'
	import FieldInput from 'winged/components/wt-model-field-input'
	import ModelUtils from 'winged/utils/wt-model-utils'
	import ModelService from 'winged/utils/wt-model-service'
	import WtDepartmentSelect from 'winged/components/wt-department-select'
	import FieldSelect from 'winged/components/wt-model-field-select'
	Vue.use(ElementUI)
	Vue.component(WtDepartmentSelect.name, WtDepartmentSelect)
	Vue.component(FieldSelect.name, FieldSelect)
	export default {
		name:"wt-new-department",
		props:{
			model: {
				type: Object,
				required: true,
			},
			value: {
				type: Object,
				default: {}
			},
			paramsData: {
				type: Object,
				default: {}
			},			
			id: {
				type: [Object, String, Number],
				default: null
			},
		},
		data(){
			return{
				form: this.value != null ? ModelUtils.cloneObject(this.value) : ModelUtils.createDefaultObject(this.model),
				isEdit:true,			
				fields:this.model.fields,
				params_data:this.paramsData,
				saving:false,
				rules:{
				}
			}
		},
		methods:{
			onSave(){

			},
			onCancel(){
				this.$emit('cancel');
			},
			change(data){
				if(data == 0){
					for(let e = 0; e < this.fields.length; e++){
						if(this.fields[e].name == 'parent_id'){
							this.fields[e].type = 1;
							this.fields[e].readonly = true;
							this.form.parent_name = '无'
						}
					}
				}else{
					for(let e = 0; e < this.fields.length; e++){
						if(this.fields[e].name == 'parent_id'){
							this.fields[e].type = 4;
							this.fields[e].readonly = false;
						}
					}
				}
			}
		},
		created(){
			if(this.id){
				this.isEdit = true;
				for(let i = 0;i<this.fields.length;i++){
					if(this.fields[i].name == 'grade'){
						if(this.value[this.fields[i].name] == 0){
							for(let e = 0; e < this.fields.length; e++){
								if(this.fields[e].name == 'parent_id'){
									this.fields[e].type = 1;
									this.fields[e].readonly = true;
									this.value.parent_id = '0'
								}
							}
						}else{
							for(let e = 0; e < this.fields.length; e++){
								if(this.fields[e].name == 'parent_id'){
								this.fields[e].type = 4;
								this.fields[e].readonly = false;
								}
							}
						}
					}
				}
			}else{
				this.isEdit = false
			}
			
			

		}
	}
</script>
<style>
	.el-form{
		max-width: 800px;
	}
	.input-max-width .el-input__inner{
		max-width: 500px;
	}
	.el-input{
		width: 360px
	}
</style>